﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NorthwindSample.Data
{
    public static class ShippersRepository
    {
        public static IList<Shipper> GetShippers()
        {
            NorthwindEntities northwind = new NorthwindEntities();
            return northwind.Shippers.ToList();
        }

        public static bool SaveShipper(Shipper shipper)
        {
            NorthwindEntities northwind = new NorthwindEntities();

            if (shipper.ShipperID > 0)
            {
                Shipper persistedShipper = northwind.Shippers.First(s => s.ShipperID == shipper.ShipperID);
                persistedShipper.Copy(shipper);
            }
            else
            {
                northwind.AddToShippers(shipper);
            }
            return northwind.SaveChanges() == 1;
        }

        public static bool DeleteShipper(int shipperID)
        {
            NorthwindEntities northwind = new NorthwindEntities();
            Shipper shipper = northwind.Shippers.First(s => s.ShipperID == shipperID);
            northwind.DeleteObject(shipper);
            return northwind.SaveChanges() == 1;
        }
    }
}
